// add map area panelId
var dopBtnAdd = "";
var dopBtnAssign = "";
var mapManager = null;
var isModify = false;
/**
 * 获取根路径basePath
 * @type 
 */
var obj=window.location; 
var contextPath=obj.pathname.split("/")[1]; 
var basePath="";
if(contextPath.indexOf(".do")==-1){
	basePath=obj.protocol+"//"+obj.host+"/"+contextPath; 
}else{
	basePath=obj.protocol+"//"+obj.host;
}
$("#dop_datagrid_deliveryCompany").datagrid({
	url : "",
	height : 350,
	pagination : true,
	fitColumns : false,
	singleSelect : false,
	fit : true,
	pageSize : 10,
	pageList : [ 10, 30, 50, 100 ],
	sortOrder : 'desc',
	rownumbers : true,
	showFooter : true,
	frozenColumns : [ [] ],
	columns : [ [ {
		field : 'ck',
		checkbox : true
	}, {
		field : "id",
		width : 150,
		title : "主键",
		hidden : true
	}, {
		field : "companyName",
		width : 150,
		title : "公司名称",
		align:'left',
		halign:"center"
	},{
		field : "statusText",
		width : 150,
		title : "状态",
		align:'center'
	},{
		field : "companyTel",
		width : 150,
		title : "公司电话",
		align:'left',
		halign:"center"
	}, {
		field : "employeeNum",
		width : 100,
		title : "员工数量",
		halign:"center",
		align:'center'
	},{
		field : "companyDesc",
		width : 270,
		title : "备注",
		halign:"center",
		align:'left'
	} ] ]
});

init();

// click pagination
var p = $("#dop_datagrid_deliveryCompany").datagrid('getPager');
$(p).pagination(
		{
			onSelectPage : function(pageNumber, pageSize) {
				var params = $.extend({
					pageNum : pageNumber,
					pageSize : pageSize
				}, getQueryParams());
				EapTools.doAction("deliveryCompanyController.do?queryList", params,
						true, function(data) {
							$("#dop_datagrid_deliveryCompany").datagrid('loadData',
									{
										"rows" : data.list,
										"total" : data.count
									} || {});
						});
			}
		});

/**
 * 修改按钮绑定事件
 */
$("#dop_btn_modify").click(function() {
	
	var selections = $("#dop_datagrid_deliveryCompany").datagrid(
			"getSelections");
	if (selections.length>0) {
		if (selections.length > 1) {
			layer.tips('请选择一行数据', this, {
				guide : 2,
				time : 2,
				more:true
			});
		} else {
			window.location.href="deliveryCompanyController.do?editCompany&isIframe&id="+selections[0].id;
			return;
			$("#companyName").val(selections[0].companyName);
			$("#companyTel").val(selections[0].companyTel);
			$("#companyDesc").val(selections[0].companyDesc);
			$("#companyId").val(selections[0].id);
			dopBtnAdd = $.layer({
						type : 1,
						title : '<div><span style="padding-left: 14px;line-height: 14px;">修改配送公司</span></div>',
						shadeClose : true,
						maxmin : false,
						fix : false,
						more:true,
						area : [ 600, 300 ],
						page : {
							dom : '#dop_deliveryCompany_add_panel'
						}
					});
		}
	} else
		layer.tips('请选择要修改行', this, {
			guide : 2,
			time : 2,
			more:true
		});
});
		
/**
 * 添加按钮绑定事件
 */
$("#dop_btn_showAdd").click(function() {
		$("#companyName").val("");
		$("#errorFlag").val("");
		$("#companyDesc").val("");
		dopBtnAdd = jQuery
				.layer({
					type : 1,
					title : '<div><span style="padding-left: 14px;line-height: 14px;">添加配送公司</span></div>',
					shadeClose : true,
					maxmin : false,
					fix : false,
					more:true,
					area : [ 600, 300 ],
					page : {
						dom : '#dop_deliveryCompany_add_panel'
					}
				});
	});


// bind delete event
$("#dop_btn_deliveryman_delete").click(function() {
	var selections = $("#dop_datagrid_deliveryCompany").datagrid("getSelections");
	if (selections && selections.length > 0)
		layer.confirm('确定删除吗？', function() {
			EapTools.doAction("deliveryCompanyController.do?delete", {
				"rowList" : JSON.stringify(selections)
			}, false, function(data) {
				if (data.success) {
					EapTip.msgOk("删除成功！");
					init();
				}
			});
		});
	else
		layer.tips('请选择要删除行', this, {
			guide : 2,
			time : 2,
			more:true
		});
});

/**
 * 启用人员
 */
$("#dop_btn_open").click(function(){
	var selections = $("#dop_datagrid_deliveryCompany").datagrid("getSelections");
	if(selections.length > 0){
		layer.confirm('确定启用吗？', function() {
			var ids=[];
			for(var i=0;i<selections.length;i++){
				ids.push(selections[i].id);
			}
			EapTools.doAction("deliveryCompanyController.do?open", {
				"ids" : ids
			}, true, function(data) {
				if (data.success) {
					EapTip.msgOk("操作成功！");
					init();
				}else{
					EapTip.msgError("操作失败，请重试！");				
				}
			});
			
		});
	}else{
		layer.tips('请选择要启用的行', this, {
			guide : 2,
			time : 2,
			more:true
		});
	}
});
/**
 * 停用人员
 */
$("#dop_btn_stop").click(function(){
	var selections = $("#dop_datagrid_deliveryCompany").datagrid("getSelections");
	if(selections.length > 0){
		layer.confirm('确定停用吗？', function() {
			var ids=[];
			for(var i=0;i<selections.length;i++){
				ids.push(selections[i].id);
			}
			EapTools.doAction("deliveryCompanyController.do?stop", {
				"ids" : ids
			}, true, function(data) {
				if (data.success) {					
					var successNumMap = data.attributes;
					if(successNumMap.num == selections.length){
						EapTip.msgOk("操作成功！");
					}else{
						EapTip.msgOk(successNumMap.num + "家公司停用成功，其他操作失败！");
					}
					init();
				}else{
					EapTip.msgError("操作失败，请重试！");				
				}
			});
			
		});
	}else{
		layer.tips('请选择要停用的行', this, {
			guide : 2,
			time : 2,
			more:true
		});
	}
});




/**
 * 验证人员描述
 */
var companyNameFlag=false;
function checkCompanyName(){
	var val=$.trim($("#companyName").val());
	if(val.length==0){
		companyNameFlag=false;
		layerComm("公司名称不得为空!","companyName");
	}else if(val.length>200){
		companyNameFlag=false;
		layerComm("公司名称不得超过100个字符!","companyName");
	}else{
		//debugger;
		companyNameFlag=true;
				
		EapTools.doAction("deliveryCompanyController.do?chekcCompanyName", {
			id:$("#companyId").val(),
			companyName : $.trim($("#companyName").val()),
			companyTel:$.trim($("#companyTel").val()),
			companyDesc:$.trim($("#companyDesc").val())
		},false,function(data) {
					//debugger;
					companyNameFlag=data.success;
					if(!data.success){
						//debugger;
						var infomap = data.attributes;
						if(infomap.expresscompanysize == 1){
							layerComm("第三方的公司数据有误，不能完成添加！","companyName");
						}
						if(infomap.deliverycompanysize == 1){
							layerComm("该公司名已经存在，不能重复添加！","companyName");
						}
						companyNameFlag=false;
					}else{
						//hideErrorMsg("delCertificatesMsg");
						companyNameFlag=true;
					}
			});
	}
}

/**
 * 验证公司电话
 */
var telFlag=false;
function checkCompanyTel(){
	var reg=/^1[3|4|5|8][0-9]\d{8}$/;
	var tel=$.trim($("#companyTel").val());
	if(tel.length==0){
		telFlag=true;
		//showErrorMsg("联系电话不能为空！","companyTelMsg");
	}else if(tel.length>20){
		telFlag=false;
		layerComm("电话长度过长！","companyTel");
		return;
	}
	telFlag=true;
}

/**
 * 验证公司备注
 */
var companyDescFlag=false;
function checkCompanyDesc(){
	var desc=$.trim($("#companyDesc").val());
	if(desc.length>300){
		layerComm("公司备注不得超过300个字符！","companyDesc");
		companyDescFlag=false;
		return;
	}
	companyDescFlag=true;
}

/***
 * layer提示信息封装
 * @param msg  消息内容
 * @param id   要显示位置的元素ID
 */
function layerComm(msg,id){
	layer.tips(msg, document.getElementById(id), {
		guide : 2,
		time : 2,
		more:true
	});
}

/**
 * 绑定keydown事件,只允许输入数字
 */
$("input[code='delMaxmum'],#delTeliPhone").keydown(function(e){
	e = e || event;	
	var code=e.keyCode;
	return (code>36&&code<41)||(code>45&&code<58)||(code>95&&code<107)||code==8||code==9;
});

// 添加面板 确定按钮

$("#dop_deliveryCompany_add_panel_ok").click(
		function() {
			var companyName = $("input[id='companyName']").val();
			//先调用方法进行校验
			checkCompanyName();
			checkCompanyTel();
			checkCompanyDesc();			
			if (!(companyNameFlag&&telFlag&&companyDescFlag)) {
				layerComm("信息添写有误，无法提交！","dop_deliveryCompany_add_panel_ok");
				return;
			}
			EapTools.doAction("deliveryCompanyController.do?saveOrUpdate", {
				companyName : $.trim($("#companyName").val()),
				companyTel:$.trim($("#companyTel").val()),
				companyDesc:$.trim($("#companyDesc").val())
			}, true, function(data) {
				/**
				 * 如果成功了，初始化控件并刷新页面
				 */
				if(data.success){
					layer.close(dopBtnAdd);
					EapTip.msgOk("操作成功！");
					$("#companyName").val("");
					$("#companyTel").val("");
					$("#companyDesc").val("");
					$("#companyId").val("");
					window.location.href="deliveryCompanyController.do?editCompany&isIframe&id="+data.obj;
				}else{
					EapTip.alertError("操作失败，请重试！");
				}
			});
		});

// 添加面板 取消按钮
$("#dop_deliveryCompany_add_panel_cancel").click(function() {
	layer.close(dopBtnAdd);
});
// 分配面板 取消按钮
$("#dop_deliveryman_assign_panel_cancel").click(function() {
	layer.close(dopBtnAssign);
});

// bind query event
$("#dop_btn_query").click(function() {
	init();
});

/*
// 绑定用户列表
EapTools.doAction("deliveryCompanyController.do?users", {}, false, function(data) {
	var selectorArray = [];
	selectorArray.push("<option value=''>---请选择---</option>");
	for (var i = 0; i < data.length; i++) {
		var thisObj = data[i];
		selectorArray.push("<option value='", thisObj.ID, "'>",
				thisObj.realname, "</option>");
	}
	$("select[id='sUser']").append(selectorArray.join(""));
});
*/
// get params
function getQueryParams() {
	return {
		companyName : $("input[code='companyName']").val()
		};
}

// init
function init() {
	// init
	var options = $("#dop_datagrid_deliveryCompany").datagrid('getPager').data(
			"pagination").options;
	var pageSize = options.pageSize;
	var params = $.extend({
		pageNum : 1,
		pageSize : pageSize
	}, getQueryParams());
	EapTools.doAction("deliveryCompanyController.do?queryList", params, true,
			function(data) {
				$("#dop_datagrid_deliveryCompany").datagrid('loadData', {
					"rows" : data.list,
					"total" : data.count
				} || {});
				var p = $("#dop_datagrid_deliveryCompany").datagrid('getPager');
				$("td:last", p).find("a").click();
			});
}
/**
 * 给company绑定失去焦点事件
 */
$("#companyName").blur(function(){
	checkCompanyName();
});